package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Transactional   //多表插入  可以保证插入的一字型
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //向菜品表插入一条数据
        dishMapper.insert(dish);
        //返回主键值
        Long dishId = dish.getId();
        //向口味表插入n跳数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorsMapper.insertBatch(flavors);
        }
    }

    /**
     *
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */

    @Override
    public PageResult get(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

       Page<DishVO> page =  dishMapper.select(dishPageQueryDTO);
        long total = page.getTotal();
        List<DishVO> result = page.getResult();
        return new PageResult(total,result);


    }

    /**
     *
     * 批量删除  可以删除一条 也可哟删除多个
     *起售中的不能删除
     * 能删除的 口味也要删除
     * 关联着套餐的也不鞥删除
     * @param ids
     */

    @Transactional
    @Override
    public void remove(List<Long> ids) {
        //判断当前菜品是否能够删除 -- 是否存在起售中的
        for (Long id : ids) {
         Dish dish = dishMapper.getById(id);
         if(dish.getStatus() == StatusConstant.ENABLE){
             //起售中  不能删除
             throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
         }
        }

        //判断菜品是否被套餐关联
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds.size()>0 && setmealIds != null){
            //关联了套餐 不能删除

            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //就删除 菜品数据
//        for (Long id : ids) {
//            dishMapper.removeById(id);
//
//            //相应的菜品口味也要删除
//            dishFlavorsMapper.removeById(id);
//        }
        dishMapper.removeBatch(ids);
        dishFlavorsMapper.removeBatch(ids);



    }

    @Override
    @Transactional
    public DishVO getById(Long id) {
        DishVO dishVO = new DishVO();
        Dish dish =   dishMapper.selectById(id);

      List<DishFlavor> list=dishFlavorsMapper.selectByDishId(id);

      BeanUtils.copyProperties(dish,dishVO);
      dishVO.setFlavors(list);
      return dishVO;
    }


    @Transactional
    @Override
    public void put(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //修改口味
        //1.删除原有的口味数据
        dishFlavorsMapper.removeById(dishDTO.getId());
        //2.插入一条口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorsMapper.insertBatch(flavors);
        }
    }

    @Override
    public void startOrStop(Dish dish) {
        //根据菜品id查询 套餐id  菜品停售 相对应的套餐也要停售
        dishMapper.update(dish);
        if (dish.getStatus() == StatusConstant.DISABLE) {
            Long dishId = dish.getId();
            List<Long> setmealList = dishMapper.selectSetmealByDishId(dishId);
            if (setmealList != null && setmealList.size() != 0){
                for (Long aLong : setmealList) {
                    //TODO 把套餐管理写完  再来补全
                    Setmeal setmeal = new Setmeal();
                    setmeal.setStatus(StatusConstant.DISABLE);
                    setmeal.setId(aLong);
                    setmealMapper.update(setmeal);
                }
            }
        }
    }

    @Override
    public List<Dish> list(Long categoryId) {

     List<Dish> dishList =   dishMapper.selectByCategoryId(categoryId);
     return dishList;
    }

    @Override
    public List<DishVO> user_selectByCategoryId(Long categoryId) {

        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);
        List<DishVO> dishVOList = dishMapper.select_user(dish);

        for (DishVO dishVO : dishVOList) {
            List<DishFlavor> dishFlavorList = dishFlavorsMapper.selectByDishId(dishVO.getId());
            dishVO.setFlavors(dishFlavorList);
        }
        return dishVOList;

    }
}
